﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class CardRechargeController : Controller
    {
        private CardRechargeInfo funinfo = new CardRechargeInfo();
        public ActionResult CardRechargeIndex()
        {
            return View();
        }

        public ActionResult GetInfo(string Select)
        {
            DataSet ds = funinfo.GetInfo(Select);
            CardRechargeVM vm = new CardRechargeVM();
            if (ds.Tables[0].Rows.Count != 0)
            {
                vm.EmpNo = ds.Tables[0].Rows[0]["EMPNO"].ToString();
                vm.DeptName = ds.Tables[0].Rows[0]["DEPTNAME"].ToString();
                vm.Name = ds.Tables[0].Rows[0]["姓名"].ToString();
                vm.Balance = ds.Tables[0].Rows[0]["卡内余额"].ToString();
            }
            return Json(vm);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string strDateTime = DateTime.Now.ToString("yyyy-MM-dd");
            IList<CardRechargeInfo> funlist = funinfo.GetList(pvm.rp, pvm.page, strDateTime);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = funinfo.GetListCount(strDateTime);
            List<string> v;
            foreach (CardRechargeInfo item in funlist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.RDateTime);
                v.Add(item.deptname);
                v.Add(item.empno);
                v.Add(item.name);
                v.Add(item.conmoney);
                v.Add(item.balance);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 保存（给卡增款）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveIncreaseMoney(CardRechargeVM model)
        {
            int result = 0;
            int countNo = 0;
            if (ModelState.IsValid)
            {
                CardRechargeInfo fun = new CardRechargeInfo()
                {
                    empno = model.HiddenEmpNo,
                    money = model.money,
                    OperatorID = ((LogOnModel)(System.Web.HttpContext.Current.Session["user"])).LogName
                };
                result = funinfo.SaveIncreaseMoney(fun);
            }
            return Json(result);
        }
    }
}
